package test3;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

public class ProducerCustomerBlockingQueue {

    /**
     * keep singleton and use blockQueue to implement pattern about consumer and producer
     */
    private static ProducerCustomerBlockingQueue queue = new ProducerCustomerBlockingQueue();

    private ProducerCustomerBlockingQueue(){

    }

    public static ProducerCustomerBlockingQueue getInstance(){
        return queue;
    }

    // total of files are 50~100
    private BlockingQueue<String> resourceQueue = new LinkedBlockingQueue(100);

    /**
     * add original data into blocking queue
     */
    public void add(String rawData){
        try {
            resourceQueue.put(rawData);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    /**
     * extract data from blocking queue
     */
    public String take(){
        String result = null;
        try {
            result = resourceQueue.take();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return result;
    }
}
